﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Utilities;
using SPPivotViewer.Utility;

namespace SPPivotViewer.Layouts.SPPivotViewer
{
    public partial class SPPivotViewerSettings : WebAdminPageBase
    {
        protected override bool RequireSiteAdministrator
        {
            get
            {
                return true;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                FillControl();
           
        }

        private void FillControl()
        {
            txtHourCache.Text = PV4SPSettings.GetValue(this.Web, PV4SPSettings.cstHourCache);
            txtMinuteCache.Text = PV4SPSettings.GetValue(this.Web, PV4SPSettings.cstMinCache);
            txtSecondCache.Text = PV4SPSettings.GetValue(this.Web, PV4SPSettings.cstSecCache);
            txtThrottling.Text = PV4SPSettings.GetValue(this.Web, PV4SPSettings.cstMaxItem);
            chk3Layer.Checked = bool.Parse(PV4SPSettings.GetValue(this.Web, PV4SPSettings.cst3Layers));
            chkOfficeWebApps.Checked = bool.Parse(PV4SPSettings.GetValue(this.Web, PV4SPSettings.cstThumbs));
            chkCustomUI.Checked = bool.Parse(PV4SPSettings.GetValue(this.Web, PV4SPSettings.cstUI));
            txtSubstituteImage.Text = PV4SPSettings.GetValue(this.Web, PV4SPSettings.cstImgSub);
            
            
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            SPUtility.Redirect("settings.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cstMaxItem, txtThrottling.Text);
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cst3Layers, chk3Layer.Checked.ToString());
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cstThumbs, chkOfficeWebApps.Checked.ToString());
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cstUI, chkCustomUI.Checked.ToString());
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cstHourCache, txtHourCache.Text);
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cstMinCache, txtMinuteCache.Text);
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cstSecCache, txtSecondCache.Text);
            PV4SPSettings.SetValue(this.Web, PV4SPSettings.cstImgSub, txtSubstituteImage.Text);
            SPUtility.Redirect("settings.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }

    }
}
